"""
This is the X-Spider downloader function
"""
from __future__ import absolute_import, unicode_literals
from celery import app
from spiders.base import BaseSpider
from celery import shared_task


@shared_task(bind=True, base=BaseSpider)
def downloader(self, url, key=None):
    print('run')
    response = self.x_request(url)
    content = response.content
    headers = response.headers
    file_type = headers.get('Content-Type')
    path = 'img/'
    if file_type.endswith('jpeg'):
        file_type = '.jpg'
    result = self.file_saver.save(content, file_type, path, key=key)
    return result
